/**
 * @author Johniak
 */
var img;
$(document).ready(function () {
	$('#profile_img').Jcrop({
		onSelect: showCoords,
		onRelease: clearCoords
	});
	$('#crop_run_modal').click(runCropModal);
	$('#save_edit_button').click(saveEdit);
	img = new Image();
	var pict = $('#profile_img');
	img.src = pict.attr('src');
});

function runCropModal() {
	$('#crop_image_modal').modal('show');
}



function showCoords(c) {
	var pict = $('#profile_img');
	$('#x').val(Math.round(c.x * img.width / pict.width()));
	$('#y').val(Math.round(c.y * img.height / pict.height()));
	$('#width').val(Math.round(c.w * img.width / pict.width()));
	$('#height').val(Math.round(c.h * img.height / pict.height()));

}

function clearCoords() {
	$('#coords input').val('');
}

function saveEdit(){
		var formData = new FormData(document.getElementById('edit_form'));
	$.ajax({
		url: '/api/hools/edit/',
		type: 'POST',
		xhr: function () {
			_xhr = $.ajaxSettings.xhr();
			if (_xhr.upload) {
				_xhr.upload.addEventListener('progress', progressHandlingFunction, false);
			}
			return _xhr;
		},
		success: createUserCompleteHandler,
		error: createUserErrorHandler,
		data: formData,
		cache: false,
		contentType: false,
		processData: false
	});
}

function progressHandlingFunction(e) {

}

function createUserCompleteHandler(response) {
 
}

function createUserErrorHandler(result) {
 
}